import React from "react";
import { Link, Outlet, Navigate, useRoutes } from "react-router-dom";

const Classify = () => {
  return <div>分类页面</div>;
};
const Hot = () => {
  return <div>热销页面</div>;
};

const Home = () => {
  return (
    <>
      <div>这是首页</div>
      <ul>
        <li>
          <Link to="/home/classify">分类页</Link>
        </li>
        <li>
          <Link to="/home/hot">热销页</Link>
        </li>
      </ul>

      <hr />
      <Outlet />
    </>
  );
};
const About = () => {
  return <div>这是关于页</div>;
};

const routes = [
  {
    path: "/",
    element: <Navigate to="/home"></Navigate>,
  },
  {
    path: "/home",
    element: <Home />,
    children: [
      {
        path: "/home",
        element: <Navigate to="/home/classify"></Navigate>,
      },
      {
        path: "/home/classify",
        element: <Classify />,
      },
      {
        path: "/home/hot",
        element: <Hot />,
      },
    ],
  },
  {
    path: "/about",
    element: <About />,
  },
];

const App = () => {
  // useRoutes允许我们写成路由表的形式，帮助我们转换成组件的形式
  const element = useRoutes(routes);
  return (
    <>
      <h2>路由表</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
      </ul>
      <ul>
        <li>
          <Link to="/about">关于页</Link>
        </li>
      </ul>

      <hr />

      {/* <Routes>
        <Route path="/" element={<Navigate to="/home"></Navigate>}></Route>
        <Route path="/home" element={<Home />}>
          <Route
            path="/home"
            element={<Navigate to="/home/classify"></Navigate>}
          ></Route>
          <Route path="/home/classify" element={<Classify />}></Route>
          <Route path="/home/hot" element={<Hot />}></Route>
        </Route>
        <Route path="/about" element={<About />}></Route>
      </Routes> */}

      {/* <Routes>
        {routes.map((item) => {
          return (
            <Route key={item.path} path={item.path} element={item.element}>
              {item.children &&
                item.children.map((value) => {
                  return (
                    <Route
                      key={value.path}
                      path={value.path}
                      element={value.element}
                    ></Route>
                  );
                })}
            </Route>
          );
        })}
      </Routes> */}

      {element}
    </>
  );
};

export default App;
